package com.tengju.payment.domain.model.transfer;

import com.tengju.payment.domain.model.AccountType;
import com.tengju.payment.domain.model.BusinessType;
import com.tengju.payment.domain.model.ChannelType;
import com.tengju.payment.domain.model.TradeStatus;
import com.tengju.payment.domain.model.channel.Channel;
import com.tengju.payment.domain.shard.DateUtil;
import com.tengju.payment.domain.shard.DomainException;
import com.tengju.payment.domain.shard.DomainExceptionCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 转账构建工厂
 */
public class TransferFactory {

    private TransferFactory() {
    }

    /**
     * 创建转账记录
     *
     * @param unionTransferRequest
     * @return
     */
    public static TransferForm createTransferForm(UnionTransferRequest unionTransferRequest) {

        if (unionTransferRequest.getBusinessNo() == null) {
            throw DomainException.error(DomainExceptionCode.BUSINESS_NO_CAN_NOT_BE_NULL);
        }

        if (unionTransferRequest.getTradeAmount() == null) {
            throw DomainException.error(DomainExceptionCode.TRADE_AMOUNT_CAN_NOT_BE_NULL);
        }

        if (!unionTransferRequest.getTradeAmount().isPositive()) {
            throw DomainException.error(DomainExceptionCode.TRADE_AMOUNT_WRONG_FORMAT, unionTransferRequest.getTradeAmount());
        }

        if (StringUtils.isBlank(unionTransferRequest.getAccountName())) {
            throw DomainException.error(DomainExceptionCode.ACCOUNT_NAME_NO_CAN_NOT_BE_NULL);
        }

        if (StringUtils.isBlank(unionTransferRequest.getAccountNo())) {
            throw DomainException.error(DomainExceptionCode.ACCOUNT_NO_CAN_NOT_BE_NULL);
        }

        if (unionTransferRequest.getAccountType() == null) {
            throw DomainException.error(DomainExceptionCode.ACCOUNT_TYPE_CAN_NOT_BE_NULL);
        }
        //对公账户校验联行号信息
        if (AccountType.COMPANY == unionTransferRequest.getAccountType()) {
            if (StringUtils.isBlank(unionTransferRequest.getBankSettleName())) {
                throw DomainException.error(DomainExceptionCode.BANK_SETTLE_NAME_CAN_NOT_BE_NULL);
            }
//            if (StringUtils.isBlank(unionTransferRequest.getBankSettleNo())) {
//                throw DomainException.error(DomainExceptionCode.BANK_SETTLE_NO_CAN_NOT_BE_NULL);
//            }
        }
        if (StringUtils.isEmpty(unionTransferRequest.getDepositoryId())) {
            throw DomainException.error(DomainExceptionCode.ACCOUNT_TYPE_CAN_NOT_BE_NULL);
        }

        //封装扩展信息
        TransferExtra transferExtra = new TransferExtra();
        transferExtra.setAccountName(unionTransferRequest.getAccountName());
        transferExtra.setAccountNo(unionTransferRequest.getAccountNo());
        transferExtra.setAccountType(unionTransferRequest.getAccountType());
        transferExtra.setBankSettleNo(unionTransferRequest.getBankSettleNo());
        transferExtra.setBankSettleName(unionTransferRequest.getBankSettleName());
        transferExtra.setCertificationType(unionTransferRequest.getCertificationTypeEnum().getCode());
        transferExtra.setDepositoryId(unionTransferRequest.getDepositoryId());

        return new TransferForm(BusinessType.TRANSFER,
                TradeStatus.CREATED,
                unionTransferRequest.getBusinessNo(),
                unionTransferRequest.getTradeAmount(),
                transferExtra);
    }

    /**
     * 根据业务流水号 构建本地交易流水
     */
    public static String createLocalTradeNo(Channel channel, BusinessNo businessNo, TransferId transferId) {
        if (ChannelType.UNION_PAY == channel.getChannelType()) {
            // 业务流水前缀 + transferId + 渠道编号 + 日期MMdd
            // 10 transferId 2 0101
            return BusinessType.TRANSFER.getNumberTradeNoPrefix()
                    + transferId.toString()
                    + (int) channel.getChannelId().getValue()
                    + DateUtil.parseDateToString(DateUtil.DATE_FORMAT_DD, new Date());
        } else if (ChannelType.CITIC_BANK == channel.getChannelType()) {
            //业务流水前缀 + 业务流水号 + 渠道编号 + 日期MMdd
            return BusinessType.TRANSFER.getTradeNoPrefix()
                    + businessNo
                    + (int) channel.getChannelId().getValue()
                    + DateUtil.parseDateToString(DateUtil.DATE_FORMAT_DD, new Date());
        } else if (ChannelType.PING_AN_DEPOSITORY == channel.getChannelType()) {
            //业务流水前缀 + 业务流水号 + 渠道编号 + 日期MMdd
            return BusinessType.DEP_TRANSFER.getTradeNoPrefix()
                    + businessNo
                    + (int) channel.getChannelId().getValue()
                    + DateUtil.parseDateToString(DateUtil.DATE_FORMAT_DD, new Date());
        } else {
            throw DomainException.error(DomainExceptionCode.CHANNEL_TYPE_NOT_SUPPORT);
        }
    }
}
